package com.yxy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yxy.common.PageResult;
import com.yxy.constant.MessageConstant;
import com.yxy.constant.StatusConstant;
import com.yxy.domain.dto.IncentiveDTO;
import com.yxy.domain.dto.IncentivePageQueryDTO;
import com.yxy.domain.po.Incentive;
import com.yxy.domain.vo.IncentivePageQueryVO;
import com.yxy.domain.vo.IncentiveVO;
import com.yxy.exception.SentenceExistException;
import com.yxy.mapper.IncentiveMapper;
import com.yxy.service.IncentiveService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class IncentiveServiceImpl implements IncentiveService {

    private final IncentiveMapper incentiveMapper;
    public IncentiveServiceImpl(IncentiveMapper incentiveMapper) {
        this.incentiveMapper = incentiveMapper;
    }

    /**
     * 添加激励
     * @param incentiveDTO
     */
    @Override
    public void add(IncentiveDTO incentiveDTO) {
        // 查询语句是否已经存在
        incentiveDTO.setSentence(incentiveDTO.getSentence().trim());
        Incentive i = incentiveMapper.getIncentiveBySentence(incentiveDTO.getSentence());
        if(i != null){
            throw new SentenceExistException(MessageConstant.SENTENCE_EXIST);
        }
        Incentive incentive = BeanUtil.copyProperties(incentiveDTO, Incentive.class);
        incentive.setSubmitTime(LocalDateTime.now());
        incentive.setStatus(StatusConstant.ENABLE);
        incentiveMapper.insert(incentive);
    }

    /**
     * 分页查询
     * @param incentivePageQueryDTO
     * @return
     */
    @Override
    public PageResult page(IncentivePageQueryDTO incentivePageQueryDTO) {
        PageHelper.startPage(incentivePageQueryDTO.getPage(), incentivePageQueryDTO.getPageSize());
        Page<IncentivePageQueryVO> page = incentiveMapper.page(incentivePageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 查询所有激励
     * @return
     */
    @Override
    public List<IncentiveVO> list() {
        return incentiveMapper.selectAll();
    }

    /**
     * 修改激励状态
     * @param id
     * @param status
     */
    @Override
    public void state(int id, int status) {
        incentiveMapper.updateState(id, status);
    }

    /**
     * 批量删除激励
     * @param ids
     */
    @Override
    public void batchDelete(List<Integer> ids) {
       incentiveMapper.deleteByIds(ids);
    }
}
